package com.nsgf.library.ui.product;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.kunminx.binding_recyclerview.adapter.BaseDataBindingAdapter;
import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.nsgf.library.data.bean.BusinessBean;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.mine.MinePointsActivity;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.zhht.htzx.BR;
import com.zhht.htzx.R;
import com.zhht.htzx.databinding.ActivityBusinessDetailBinding;
import com.zhht.htzx.databinding.ItemBusinessGoodsBinding;


/**
 * 商户详情
 */
public class BusinessDetailActivity extends BaseActivity {

    private NewComersStates mStates;

    private AccountRequester mAccountRequester;
    private SimpleDataBindingAdapter<BusinessBean.ProductListBean, ItemBusinessGoodsBinding> mAdapter;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(NewComersStates.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new SimpleDataBindingAdapter<BusinessBean.ProductListBean, ItemBusinessGoodsBinding>(this, R.layout.item_business_goods, new DiffUtil.ItemCallback<BusinessBean.ProductListBean>() {
            @Override
            public boolean areItemsTheSame(@NonNull BusinessBean.ProductListBean oldItem, @NonNull BusinessBean.ProductListBean newItem) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(@NonNull BusinessBean.ProductListBean oldItem, @NonNull BusinessBean.ProductListBean newItem) {
                return false;
            }
        }) {
            @Override
            protected void onBindItem(ItemBusinessGoodsBinding binding, BusinessBean.ProductListBean item, RecyclerView.ViewHolder holder) {
                binding.setVm(item);
            }
        };
        return new DataBindingConfig(R.layout.activity_business_detail, BR.vm, mStates)
                .addBindingParam(BR.adapter, mAdapter);
    }

    private ActivityBusinessDetailBinding mBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLifecycle().addObserver(mAccountRequester);
        mBinding = (ActivityBusinessDetailBinding) getBinding();
        mStates.titleBar.set(getIntent().getStringExtra("title"));
        mBinding.titlebar.titleBarRoot.setTextColor(getResources().getColor(R.color.white));
        mBinding.titlebar.titleBarBack.setImageResource(R.drawable.ic_titlebar_white_back);

        mAccountRequester.queryTenantDetail(getIntent().getStringExtra("id"));

        mAdapter.setOnItemClickListener(new BaseDataBindingAdapter.OnItemClickListener<BusinessBean.ProductListBean>() {
            @Override
            public void onItemClick(int viewId, BusinessBean.ProductListBean item, int position) {

                startActivity(new Intent()
                        .putExtra("id",item.getIdX())
                        .setClass(BusinessDetailActivity.this, ProductDetailActivity.class));
            }
        });

        mAccountRequester.getBusinessProductResult().observe(this, new Observer<ResponseStatus<BusinessBean>>() {
            @Override
            public void onChanged(ResponseStatus<BusinessBean> businessBeanResponseStatus) {
                if (businessBeanResponseStatus.getResponseCode() == 0) {
                    mStates.businessBeanState.set(businessBeanResponseStatus.getData());

                    mStates.name.set(businessBeanResponseStatus.getData().getZhhtMerchantsType() == 3 ? "自营店铺" : "联盟店铺");
                }
            }
        });

    }

    public static class NewComersStates extends StateHolder {
        public final State<BusinessBean> businessBeanState = new State<>(new BusinessBean());
    }
}
